﻿
CREATE FUNCTION [dbo].[GetCantitateComandataSiReceptionata]
(
	@IDDocumentDetaliuReceptie bigint,
	@IDArticol int	
)
RETURNS decimal(25,5)
AS
BEGIN


DECLARE @ResultVar decimal(25,5)

	SELECT @ResultVar = COALESCE(SUM(ddl.Cantitate),0) FROM DocumenteDetaliu dd JOIN
	Documente d ON d.IDDocument=dd.IDDocument
	JOIN DocumenteDetaliuLink  ddl ON ddl.IDDocDetaliuSursa = dd.IDDocumentDetaliu
	AND ddl.IDDocDetaliuDest = @IDDocumentDetaliuReceptie AND dd.IDArticol=@IDArticol AND 
	(d.IDTipDocument = 9 OR d.IDTipDocument = 12) AND ddl.Tip='CM'

	-- Return the result of the function
	RETURN @ResultVar

END